package com.v2future.sjt.collegecheckingattendance.event.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.v2future.sjt.collegecheckingattendance.R;
import com.v2future.sjt.collegecheckingattendance.utils.DensityUtil;

import org.xutils.image.ImageOptions;
import org.xutils.x;

import java.util.List;

/**
 * Created by Administrator on 2016/1/15.
 */
public class EventImgGridAdapter extends BaseAdapter {

    List<String> dataList;
    Context mContext;
    ImageOptions imageOptions;

    public EventImgGridAdapter(Context context, List<String> dataList) {
        this.mContext = context;
        this.dataList = dataList;
    }

    @Override
    public int getCount() {
        return dataList.size();
    }

    @Override
    public Object getItem(int position) {
        return dataList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        String bean = dataList.get(position);
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(
                    R.layout.griditem_event_img, null);
            holder = new ViewHolder();
            holder.urlImageIv = (ImageView) convertView.findViewById(R.id.iv_urlimg);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        x.image().bind(holder.urlImageIv, bean, getImageOptions());
        return convertView;
    }

    public class ViewHolder {
        ImageView urlImageIv;
    }

    public ImageOptions getImageOptions(){
        imageOptions = new ImageOptions.Builder()
                .setSize(DensityUtil.dip2px(mContext, 120), DensityUtil.dip2px(mContext, 120))
                .setImageScaleType(ImageView.ScaleType.CENTER_CROP)
                .setFailureDrawableId(R.mipmap.ic_img_load_error)
                .setLoadingDrawableId(R.mipmap.ic_album)
                .build();
        return imageOptions;
    }
}
